package org.alvin.gencode;

import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.api.scripting.ScriptObjectMirror;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TableColumnTransferService {

    public String transfer(String src){
        src = src.substring(1,src.length() -2);
        String render = "";
        String regex = "([{](.+)[}])";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(src);
        StringBuilder sb = new StringBuilder();
        while(m.find()){
            JSONObject jsonObject =  JSONObject.parseObject(m.group(1));
            String tag = "<el-table-column property=\"{0}\" label=\"{1}\" />";
            sb.append(MessageFormat.format(tag , jsonObject.getString("key"),jsonObject.getString("title"))).append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) throws ScriptException {
//        String text = " [\n" +
//                "        {\n" +
//                "          title: \"编号\",\n" +
//                "          render: (h, val) => {\n" +
//                "            return h(\"span\", 1 + val.index + (this.page - 1) * this.pageSize);\n" +
//                "          },\n" +
//                "          width: 80,\n" +
//                "        },\n" +
//                "        { title: \"运营商\", key: \"ogranName\" },\n" +
//                "        { title: \"简称\", key: \"shortName\" },\n" +
//                "        { title: \"上级\", key: \"parentName\" },\n" +
//                "        { title: \"创建时间\", key: \"createTime\" },\n" +
//                "        {\n" +
//                "          title: \"单车站数\",\n" +
//                "          key: \"bikeStationNum\",\n" +
//                "          slot: \"col_bikeStationNum\",\n" +
//                "          width: 120,\n" +
//                "        },\n" +
//                "        {\n" +
//                "          title: \"汽车站数\",\n" +
//                "          key: \"carStationNum\",\n" +
//                "          slot: \"col_carStationNum\",\n" +
//                "          width: 120,\n" +
//                "        },\n" +
//                "        {\n" +
//                "          title: \"停车场数\",\n" +
//                "          key: \"parkingStationNum\",\n" +
//                "          slot: \"col_parkingStationNum\",\n" +
//                "          width: 120,\n" +
//                "        },\n" +
//                "        {\n" +
//                "          title: \"单车桩数\",\n" +
//                "          key: \"bikePileTenPortNum\",\n" +
//                "          slot: \"col_bikePileTenPortNum\",\n" +
//                "          width: 120,\n" +
//                "        },\n" +
//                "        {\n" +
//                "          title: \"汽车桩数\",\n" +
//                "          key: \"carPileNum\",\n" +
//                "          slot: \"col_carPileNum\",\n" +
//                "          width: 120,\n" +
//                "        },\n" +
//                "        { title: \"车位锁数\", key: \"lockNum\", slot: \"col_lockNum\", width: 120 },\n" +
//                "        {\n" +
//                "          title: this.$t(\"ParkingLockList.caozuo\"),\n" +
//                "          width: 200,\n" +
//                "          slot: \"col_opt\",\n" +
//                "          fixed: \"right\",\n" +
//                "        },\n" +
//                "      ]";

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        String script = "" +

                "var obj = {\n" +
                "      collapsed: true,\n" +
                "      tableHeader: [\n"  +
                "        { title: \"运营商\", key: \"ogranName\" },\n" +
                "        { title: \"简称\", key: \"shortName\" },\n" +
                "        { title: \"上级\", key: \"parentName\" },\n" +
                "        { title: \"创建时间\", key: \"createTime\" },\n" +
                "        {\n" +
                "          title: \"单车站数\",\n" +
                "          key: \"bikeStationNum\",\n" +
                "          slot: \"col_bikeStationNum\",\n" +
                "          width: 120,\n" +
                "        },\n" +
                "        {\n" +
                "          title: \"汽车站数\",\n" +
                "          key: \"carStationNum\",\n" +
                "          slot: \"col_carStationNum\",\n" +
                "          width: 120,\n" +
                "        },\n" +
                "        {\n" +
                "          title: \"停车场数\",\n" +
                "          key: \"parkingStationNum\",\n" +
                "          slot: \"col_parkingStationNum\",\n" +
                "          width: 120,\n" +
                "        },\n" +
                "        {\n" +
                "          title: \"单车桩数\",\n" +
                "          key: \"bikePileTenPortNum\",\n" +
                "          slot: \"col_bikePileTenPortNum\",\n" +
                "          width: 120,\n" +
                "        },\n" +
                "        {\n" +
                "          title: \"汽车桩数\",\n" +
                "          key: \"carPileNum\",\n" +
                "          slot: \"col_carPileNum\",\n" +
                "          width: 120,\n" +
                "        },\n" +
                "        { title: \"车位锁数\", key: \"lockNum\", slot: \"col_lockNum\", width: 120 },\n" +
                "        {\n" +
                "          title: this.$t(\"ParkingLockList.caozuo\"),\n" +
                "          width: 200,\n" +
                "          slot: \"col_opt\",\n" +
                "          fixed: \"right\",\n" +
                "        },\n" +
                "      ],\n" +
                "      total: 0,\n" +
                "      page: 1,\n" +
                "      pageSize: 10,\n" +
                "      dataList: [],\n" +
                "      loading: false,\n" +
                "      form: this.initForm(),\n" +
                "      collapsed: false,\n" +
                "      statistics: {},\n" +
                "    }";
        engine.eval(script);
        ScriptObjectMirror obj = (ScriptObjectMirror)engine.get("obj");
        System.out.println(obj.get("tableHeader"));;

//        System.out.println(new TableColumnTransferService().transfer(text));

    }
}
